package com.myz.rabbitmq.study3;

import com.myz.rabbitmq.utils.RabbitMqUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

public class Worker03 {
    public static final String TASK_QUEUE_NAME = "ack_queue";

    public static void main(String[] args) throws Exception {
        // 1获取信道
        Channel channel = RabbitMqUtils.getChannel();
        System.out.println("C1接收消息间隔为1s");
        // 接收回调
        DeliverCallback deliverCallback = (consumerTag,message) ->{
          // 程序睡眠1秒
            try {
                Thread.sleep(1000);
                System.out.println("C1接收到的消息:"+new String(message.getBody(),"UTF-8"));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            // 手动应答:参数1,消息的标记tag 参数2,是否批量应答 boolean
            channel.basicAck(message.getEnvelope().getDeliveryTag(),false);
        };
        // 设置不公平分发  0:轮训分发 1:不公平分发
//        channel.basicQos(1);
        // 预取值 basicQos默认为0轮训分发，设置为1为不公平分发，设置为其它值时为预取值
        channel.basicQos(5);
        // 采用手动应答
        boolean autoAck = false;
        channel.basicConsume(TASK_QUEUE_NAME,autoAck,deliverCallback,(consumerTag)->{
            System.out.println(consumerTag+"消费者取消消费接口回调逻辑");
        });
    }
}
